# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     serializers.py
    Description:   自定义序列化器
 -------------------------------------------------
 """
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from django.contrib.auth.models import User as Users

__all__ = ['CustomModelSerializer']


class CustomModelSerializer(ModelSerializer):
    # # 修改人的审计字段名称, 默认modifier, 继承使用时可自定义覆盖
    # modifier_field_id = "modifier"
    # modifier_name = serializers.SerializerMethodField(read_only=True)
    #
    # def get_modifier_name(self, instance):
    #     if not hasattr(instance, "modifier"):
    #         return None
    #     queryset = (
    #         Users.objects.filter(id=instance.modifier)
    #         .values_list("username", flat=True)
    #         .first()
    #     )
    #     if queryset:
    #         return queryset
    #     return None

    # 添加默认时间返回格式
    create_datetime = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False,
        # read_only=True,
        write_only=True
    )
    update_datetime = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False,
        # read_only=True,
        write_only=True
    )
